// const Lunar = require("lunar-javascript");
const express = require("express");
const router = express.Router();
const { Solar, LunarYear } = require("lunar-javascript");

// 生成当前时间的所有 公里 农历信息
router.get("/", function (req, res) {
  const now = new Date();
  const model = {
    year: "",
    month: "",
    day: ""
  };
  const state = {
    lang: "chs",
    year: now.getFullYear(),
    month: now.getMonth() + 1,
    day: now.getDate(),
    yearGanZhi: "",
    yearShengXiao: "",
    yearNaYin: "",
    lunarYearInChinese: "",
    lunarMonthInChinese: "",
    lunarDayInChinese: "",
    yearZhiShui: "",
    yearGenTian: "",
    yearDeJin: "",
    yearFenBing: "",
    yearKongWang: "",
    monthGanZhi: "",
    monthShengXiao: "",
    monthNaYin: "",
    monthTaiShen: "",
    monthKongWang: "",
    dayGanZhi: "",
    dayShengXiao: "",
    dayNaYin: "",
    dayTaiShen: "",
    dayZhiShen: "",
    dayChong: "",
    dayTianShen: "",
    daySha: "",
    dayLu: "",
    dayJiShen: [],
    dayXiongSha: [],
    dayYi: [],
    dayJi: [],
    dayKongWang: "",
    dayJiuXing: "",
    prevJq: "",
    nextJq: "",
    pengZuGan: "",
    pengZuZhi: "",
    liuYao: "",
    yueXiang: "",
    yueMing: "",
    wuHou: "",
    weekInChinese: "",
    xingZuo: "",
    xiu: "",
    positionFu: "",
    positionCai: "",
    positionXi: "",
    positionYangGui: "",
    positionYinGui: ""
  };

  function render() {
    model.year = state.year + "";
    model.month = state.month + "";
    model.day = state.day + "";
    const solar = Solar.fromYmd(state.year, state.month, state.day);

    state.weekInChinese = solar.getWeekInChinese();
    state.xingZuo = solar.getXingZuo();

    const lunar = solar.getLunar();
    state.yearGanZhi = lunar.getYearInGanZhi();
    state.yearShengXiao = lunar.getYearShengXiao();
    state.yearNaYin = lunar.getYearNaYin();
    state.lunarYearInChinese = lunar.getYearInChinese();
    state.yearKongWang = lunar.getYearXunKong();

    state.monthGanZhi = lunar.getMonthInGanZhi();
    state.monthShengXiao = lunar.getMonthShengXiao();
    state.monthNaYin = lunar.getMonthNaYin();
    state.monthTaiShen = lunar.getMonthPositionTai();
    state.lunarMonthInChinese = lunar.getMonthInChinese();
    state.monthKongWang = lunar.getMonthXunKong();

    state.yueXiang = lunar.getYueXiang();
    state.wuHou = lunar.getHou() + " " + lunar.getWuHou();
    state.yueMing = lunar.getSeason();

    state.dayGanZhi = lunar.getDayInGanZhi();
    state.dayShengXiao = lunar.getDayShengXiao();
    state.dayNaYin = lunar.getDayNaYin();
    state.dayTaiShen = lunar.getDayPositionTai();
    state.pengZuGan = lunar.getPengZuGan();
    state.pengZuZhi = lunar.getPengZuZhi();
    state.dayZhiShen = lunar.getZhiXing();
    state.dayTianShen =
      lunar.getDayTianShen() + "(" + lunar.getDayTianShenType() + "日)";
    state.daySha = lunar.getDaySha();
    state.liuYao = lunar.getLiuYao();
    state.dayLu = lunar.getDayLu();
    state.dayJiShen = lunar.getDayJiShen();
    state.dayXiongSha = lunar.getDayXiongSha();
    state.dayYi = lunar.getDayYi();
    state.dayJi = lunar.getDayJi();
    state.lunarDayInChinese = lunar.getDayInChinese();

    const dayNineStar = lunar.getDayNineStar();
    state.dayJiuXing = dayNineStar.toString();

    const prevJq = lunar.getPrevJieQi(true);
    const prevJqSolar = prevJq.getSolar();
    state.prevJq =
      prevJq.getName() +
      "：" +
      prevJqSolar.toYmd() +
      " 周" +
      prevJqSolar.getWeekInChinese();

    const nextJq = lunar.getNextJieQi(true);
    const nextJqSolar = nextJq.getSolar();
    state.nextJq =
      nextJq.getName() +
      "：" +
      nextJqSolar.toYmd() +
      " 周" +
      nextJqSolar.getWeekInChinese();

    state.dayChong = lunar.getDayShengXiao() + "日冲" + lunar.getDayChongDesc();
    state.dayKongWang = lunar.getDayXunKong();

    state.xiu =
      lunar.getGong() +
      "方" +
      lunar.getXiu() +
      lunar.getZheng() +
      lunar.getAnimal() +
      "(" +
      lunar.getXiuLuck() +
      ")";

    state.positionXi = lunar.getPositionXiDesc();
    state.positionFu = lunar.getPositionFuDesc();
    state.positionCai = lunar.getPositionCaiDesc();
    state.positionYangGui = lunar.getPositionYangGuiDesc();
    state.positionYinGui = lunar.getPositionYinGuiDesc();

    const lunarYear = LunarYear.fromYear(lunar.getYear());
    state.yearZhiShui = lunarYear.getZhiShui();
    state.yearDeJin = lunarYear.getDeJin();
    state.yearGenTian = lunarYear.getGengTian();
    state.yearFenBing = lunarYear.getFenBing();
  }

  render();

  res.status(200).send({
    code: 200,
    data: state,
    messgae: "success"
  });
});

router.get("/info", function (req, res) {
  const now = new Date();

  const state = {
    year: now.getFullYear(),
    month: now.getMonth() + 1,
    day: now.getDate()
  };
  let solar = Solar.fromYmd(state.year, state.month, state.day);
  const lunar = solar.getLunar();
  res.status(200).send({
    code: 200,
    data: {
      full: solar.toFullString(),
      lunar: solar.getLunar().toFullString(),
      dayYi: lunar.getDayYi(),
      dayJi: lunar.getDayJi(),
      lunarYearInChinese: lunar.getYearInChinese(),
      lunarMonthInChinese: lunar.getMonthInChinese(),
      lunarDayInChinese: lunar.getDayInChinese(),
      weekInChinese: solar.getWeekInChinese(),
      ...state
    },
    messgae: "success"
  });
});

module.exports = router;
